package org.wisigoth.chat.client;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
 * Factory creating an nstance of Wisigoth
 * @author tof
 *
 */
public class WisigothFactory {
	
	private static final String WISIGOTH_CLASS_IMPL_PROPERTY = "org.wisigoth.chat.client.impl";
/**
 * Method creating a unique instance of Wisigoth according to a implementation defined in
 * the properties file : wisigoth.properties	
 * @return a {@link Wisigoth}
 */
	public static Wisigoth createInstance(){
		String WisigothImplClass = getClassName();
		if(WisigothImplClass == null){
			throw new IllegalStateException("Wisigoth implementation class not found in wisigoth.properties");
		}
		Wisigoth result = null;
		try {
			Class<Wisigoth> wisigothClass =(Class<Wisigoth>)Class.forName(WisigothImplClass);
			result = wisigothClass.newInstance();
		} catch (Exception e) {
			throw new IllegalStateException("Wisigoth implementation class not found: " + WisigothImplClass, e);
		}
		
		return result;
		//return new WisigothMockUp();
	}

	private static String getClassName() {
		String result = null;
		Properties wisigothProps = new Properties();
		InputStream propertiesStream = WisigothFactory.class.getResourceAsStream("/wisigoth.properties");
		if(propertiesStream == null){
			throw new IllegalStateException("wisigoth.properties not found in Classpath");
		}
		try {
			wisigothProps.load(propertiesStream);
			result = wisigothProps.getProperty(WISIGOTH_CLASS_IMPL_PROPERTY);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	
}
